<?php

/**
 * This is the model class for table "{{url_class}}".
 *
 * The followings are the available columns in table '{{url_class}}':
 * @property integer $classid
 * @property integer $parentid
 * @property string $classname
 * @property integer $displayorder
 * @property integer $sitenum
 * @property string $path
 * @property string $keywords
 * @property string $description
 * @property integer $create_at
 * @property integer $update_at
 * @property integer $level
 * @property string $classroot
 */
class UrlClass extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UrlClass the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{url_class}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('classname', 'required'),
			array('parentid, displayorder, sitenum, create_at, update_at, level', 'numerical', 'integerOnly'=>true),
			array('classname', 'length', 'max'=>20),
			array('path', 'length', 'max'=>255),
			array('keywords', 'length', 'max'=>100),
			array('classroot', 'length', 'max'=>200),
			array('description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('classid, parentid, classname, displayorder, sitenum, path, keywords, description, create_at, update_at, level, classroot', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'classid' => 'Classid',
			'parentid' => 'Parentid',
			'classname' => 'Classname',
			'displayorder' => 'Displayorder',
			'sitenum' => 'Sitenum',
			'path' => 'Path',
			'keywords' => 'Keywords',
			'description' => 'Description',
			'create_at' => 'Create At',
			'update_at' => 'Update At',
			'level' => 'Level',
			'classroot' => 'Classroot',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('classid',$this->classid);
		$criteria->compare('parentid',$this->parentid);
		$criteria->compare('classname',$this->classname,true);
		$criteria->compare('displayorder',$this->displayorder);
		$criteria->compare('sitenum',$this->sitenum);
		$criteria->compare('path',$this->path,true);
		$criteria->compare('keywords',$this->keywords,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('create_at',$this->create_at);
		$criteria->compare('update_at',$this->update_at);
		$criteria->compare('level',$this->level);
		$criteria->compare('classroot',$this->classroot,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	/**
	 * @todo 根据classId获取分类的父类
	 * @param Int $classid分类ID
	 * @param int $out_type 0=返回字符串 1=返回数组
	 * @param string $glue 分类连接符
	 * @author wanghan 王汉
	 */
	public function getClassNames($classid = 0,$out_type = 0 ,$glue = '>')
	{
		$ret_class_names = array();
		if($classid != 0){
			do
			{
				$tmp_model = self::model()->findByPk($classid);
				if($tmp_model){
					$ret_class_names[$tmp_model->classid] = $tmp_model->classname;
					$parent_id = $tmp_model->parentid;
					$classid = $parent_id;
				}
				$parent_id = 0;			
			} while ($parent_id > 0 );
		}
		$ret_class_names = array_reverse($ret_class_names,true);
		if($out_type == 1){
			return $ret_class_names;
		}
		if($out_type == 0){
			return implode($glue, $ret_class_names);
		}
	}
	
	/**
	 * 获取子类
	 * @param int $classid 分类id
	 * @return array 
	 * @author wanghan
	 */
	public function getSubClass($classid){
		$condition = "parentid=".$classid;
		$models = self::model()->findAll($condition);
		$returnArr = array();
		foreach ($models as $model) {
			$returnArr[$model->classid] = $model->classname;
		}
		return $returnArr;
	}
}